//
//  HYAttribute.m
//  HYHoney
//
//  Created by dllo on 16/1/19.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "HYAttribute.h"
#import "RESideMenu.h"
#import "UIImageView+WebCache.h"
#import "shoucangView.h"
#import "DKNightVersion.h"

@interface HYAttribute ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain)UITableView *tableView;
@property (nonatomic,retain)UIImageView *backgroundview;
@property (nonatomic,retain)NSMutableArray *array;

@property (nonatomic, retain)NSMutableArray *arrfile;
@property (nonatomic, assign)int count;
@property (nonatomic, retain)NSString *str;
@property (nonatomic,retain)UISwitch *sw;

@end

@implementation HYAttribute

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    self.view.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"szblack.jpeg"]];
    
//    self.backgroundview = [UIImage imageNamed: DKImageWithImages([UIImage imageNamed:@"grblack"], [UIImage imageNamed:@"szblack"])];
    
    
    self.navigationItem.title = @"设置";
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:1 green:1 blue:1 alpha:1], NSForegroundColorAttributeName, nil]];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"xial.png"] style:UIBarButtonItemStylePlain target:self action:@selector(presentLeftMenuViewController:)];
    
    [self createTableView];
    [self handleData];
}


- (void)handleData{

    self.str = [NSString stringWithFormat:@"%0.2fM", ([[SDImageCache sharedImageCache] getSize] / (1024.0 * 1024.0))];
    
    self.array = @[@[@{@"name":@"条款声明"},
                     @{@"name":@"当前版本"}],
                   @[@{@"name":@"清空缓存",@"number": self.str},
                     @{@"name":@"我的收藏"},
                     @{@"name":@"夜间模式"}]].mutableCopy;
}


- (void)createTableView{

    self.tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
    
    self.tableView.dk_backgroundColorPicker = DKColorWithRGB(0xFFFAFA, 0x27408B);

    [self.view addSubview:self.tableView];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.array.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSArray *arr = self.array[section];
    return arr.count;

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *pool1 = @"resuse";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:pool1];
    
    
    if (cell == nil) {
        
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:pool1] autorelease];
        cell.dk_backgroundColorPicker = DKColorWithRGB(0xFFF0F5, 0x27408B);

        cell.textLabel.dk_textColorPicker = DKColorWithRGB(0x000000, 0x87ceff);
    }
    
    NSArray *arr = [self.array objectAtIndex:indexPath.section];
    NSDictionary *dic = arr[indexPath.row];
    cell.textLabel.text = [dic objectForKey:@"name"];
    cell.detailTextLabel.text = [dic objectForKey:@"number"];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    
    if (indexPath.section == 1 && indexPath.row == 2 ) {
        
        self.sw = [[UISwitch alloc] initWithFrame:CGRectZero];
        
        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"ISNIGHT"]) {
            self.sw.on = YES;
        }else {
            self.sw.on = NO;
            
        }
        [self.sw addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
        cell.accessoryView = self.sw;
        
        [self.view addSubview:self.sw];
        
    }
    
    
    return cell;

}


- (void)switchAction:(UISwitch *)sw{
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];

    if (self.sw.on && [DKNightVersionManager currentThemeVersion] == DKThemeVersionNormal) {
        
        [DKNightVersionManager nightFalling];
    } else {
        [DKNightVersionManager dawnComing];
    }
    
    [user setBool:sw.on forKey:@"ISNIGHT"];
    [user synchronize];
}


- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    [super setEditing:editing animated:animated];
    
    
    /**让 TableView 进入/退出 编辑状态*/
    
    [self.tableView setEditing:editing animated:animated];
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        
        NSLog(@"111");
        
    }else if (indexPath.section == 0 && indexPath.row ==1){
        
        NSLog(@"222");
        
        
    }else if (indexPath.section == 1 && indexPath.row == 0) {
        
        
        [self performSelectorOnMainThread:@selector(clear) withObject:nil waitUntilDone:YES];
        
    }else if (indexPath.section == 1 && indexPath.row == 1){
        
        shoucangView *shou = [[shoucangView alloc]init];
        
        [self.navigationController pushViewController:shou animated:YES];
        
    }else {
    
        
    }
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (void)clear{
    NSLog(@"清理成功");
    
    [[SDImageCache sharedImageCache] clearDisk];

    self.str = [NSString stringWithFormat:@"%0.2fM", ([[SDImageCache sharedImageCache] getSize] / (1024.0 * 1024.0))];
    self.array = @[@[@{@"name":@"条款声明"},
                     @{@"name":@"当前版本"}],
                   @[@{@"name":@"清空缓存",@"number": self.str},
                     @{@"name":@"我的收藏"},
                     @{@"name":@"夜间模式"}]].mutableCopy;

    [self.tableView reloadData];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
